package service

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"fx_swift_ifcgate/api"
	"fx_swift_ifcgate/internal/model"
	"fx_swift_ifcgate/pkg/code"
	"fx_swift_ifcgate/pkg/global"
	"fx_swift_ifcgate/pkg/httputils"
	"fx_swift_ifcgate/pkg/internalerrors"
	"fx_swift_ifcgate/pkg/logger"
	"fx_swift_ifcgate/pkg/utils"
	"github.com/gogf/gf/v2/container/gvar"
	"github.com/gogf/gf/v2/frame/g"
	"regexp"
	"sort"
	"strings"
)

// va开户列表根据状态排序
func SortByStatus(list []*model.IFCGateVAInfoCallBack) []*model.IFCGateVAInfoCallBack {
	sort.Slice(list, func(i, j int) bool {
		return list[i].Status == "ON" && list[j].Status != "ON"
	})
	return list
}

func VerifyEmailFormat(email string) bool {
	pattern := `\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*` //匹配电子邮箱
	reg := regexp.MustCompile(pattern)
	return reg.MatchString(email)
}

// 获取国家信息
func GetCountryInfo(ctx context.Context) ([]*api.GetCountryRes, error) {
	var (
		err         error
		countryList []*model.FxCountry
	)
	if err = global.GFDB.Model("fx_country").Scan(&countryList); err != nil {
		logger.SugarLogger.Error(err)
		return nil, internalerrors.New(code.ErrUnknown, "数据查询异常")
	}
	result := make([]*api.GetCountryRes, 0)
	for _, value := range countryList {
		result = append(result, &api.GetCountryRes{
			Id: value.ID,
			NameInfo: api.NameInfo{
				ChineseName: value.ChineseName,
				EnglishName: value.EnglishName,
			},
			CountryCode:    value.CountryCode,
			IsoCountryCode: value.IsoCountryCode,
			Status:         value.Status,
		})
	}
	return result, err
}

// 获取币种对应的编码和中文
func GetCurrencyInfo(ctx context.Context) ([]*api.GetCurrencyInfoRes, error) {
	var (
		err          error
		currencyList []*model.IfcgateCurrency
	)
	if err = global.GFDB.Model("ifcgate_currency").Scan(&currencyList); err != nil {
		logger.SugarLogger.Error(err)
		return nil, internalerrors.New(code.ErrUnknown, "数据查询异常")
	}
	result := make([]*api.GetCurrencyInfoRes, 0)
	for _, value := range currencyList {
		result = append(result, &api.GetCurrencyInfoRes{
			CurrencyCode: value.CurrencyCode,
			CurrencyName: value.CurrencyName,
			Image:        value.Image,
		})
	}
	return result, err
}

// 从上下文中获取ottPayToken
func GetMerchantToToken(ctx context.Context) (string, error) {
	var (
		err         error
		redisResult *gvar.Var
		merchantNo  string
	)
	merchantNo = ctx.Value("MerchantNo").(string)
	redisResult, err = g.Redis().Get(ctx, merchantNo)
	if err != nil || redisResult.Val() == nil {
		logger.SugarLogger.Error(err)
		return "", nil
	}
	return redisResult.String(), err
}

// 通过商户号获取对应的代理商
func GetAccountToMerchantNo(ctx context.Context) (string, error) {
	var (
		err            error
		merchantNo     string
		companyUserObj *model.IfcgateCompanyUser
	)
	merchantNo = ctx.Value("MerchantNo").(string)
	if err = global.GFDB.Ctx(ctx).Model("ifcgate_company_user").
		Where("merchant_no=?", merchantNo).Scan(&companyUserObj); err != nil {
		logger.SugarLogger.Error(err)
		return "", nil
	}
	return companyUserObj.AgentAccount, err
}

// 请求OTTPay参数封装
func OperatorParamToPost(tradeCode, accessToken string, body []byte) ([]byte, error) {
	var (
		err  error
		resp string
		url  string
	)
	url = global.ViperObj.GetString("ottpay.baseUrl") + tradeCode
	//组装请求头参数
	reqMap := httputils.GetRequestParam(tradeCode, accessToken)
	//组装请求体参数
	reqBody := make(map[string]interface{})
	err = json.Unmarshal(body, &reqBody)
	if err != nil {
		return nil, err
	}
	reqMap["body"] = reqBody

	//转换成byte
	jsonData, err := json.Marshal(reqMap)
	//请求参数加密
	request := utils.Encrtpt(string(jsonData), global.ViperObj.GetString("ottpay.merChantNo"))
	reqJson, _ := json.Marshal(request)
	resp = httputils.DoPost(url, reqJson)
	if resp == "" {
		logger.SugarLogger.Errorf("响应报文为空:%s", resp)
		return nil, errors.New("响应报文为空")
	}
	var resBean = &api.Bean{}
	err = json.Unmarshal([]byte(resp), &resBean)
	if err != nil {
		logger.SugarLogger.Errorf("json 读取报错:%w", err)
		return nil, err
	}
	//解密并且封装实体
	resStr, err := utils.Decrypt(resBean)
	if err != nil {
		logger.SugarLogger.Errorf("响应报文解密报错:%w", err)
		return nil, err
	}
	headObj := &api.JsonStr{}
	err = json.Unmarshal(resStr, &headObj)
	//校验返回结果是否有异常，有则返回异常信息
	if headObj.Head.RespDesc != "请求成功" && headObj.Head.RespDesc != "S00000" {
		return nil, errors.New(headObj.Head.RespDesc)
	}
	return resStr, err
}

func ConvertMerChantReq(req *api.MerChantVAReq) (*api.MerChantVAReq, error) {
	if req == nil {
		return nil, internalerrors.New(code.ErrUnknown, "请求参数为空")
	}
	converReq := req
	byteReq, _ := json.Marshal(converReq)
	reqStr := string(byteReq)

	var filePath string
	tarGetPath := global.ViperObj.GetString("sftp.targetPath")
	if converReq.CountryCode == "CN" {
		if len(converReq.EnterprisePath) == 0 {
			return nil, internalerrors.New(code.ErrUnknown, "全国工商企业文件不能为空")
		}
		filePath = converReq.EnterprisePath[0]
	} else {
		if len(converReq.CompanyCertificate) == 0 {
			return nil, internalerrors.New(code.ErrUnknown, "公司注册证书不能为空")
		}
		filePath = converReq.CompanyCertificate[0]
	}
	//将req中的上传路径替换成sftp的路径
	lastPath := strings.LastIndex(filePath, "/")
	subStr := filePath[:lastPath]
	reqStr = strings.ReplaceAll(reqStr, subStr, tarGetPath)

	var reqObj *api.MerChantVAReq
	err := json.Unmarshal([]byte(reqStr), &reqObj)
	if err != nil {
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	return reqObj, nil
}

func PostTp1021(req *api.Tp1021Req, merchantToken string) (*api.Tp1021Res, error) {
	var (
		err    error
		body   []byte
		resStr []byte
	)
	body, err = json.Marshal(&req)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	resStr, err = OperatorParamToPost("tp1021", merchantToken, body)
	if err != nil {
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	ottPayResult := &api.Tp1021Res{}
	err = json.Unmarshal(resStr, &ottPayResult)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	if ottPayResult.Head.RespDesc == "请求成功" && ottPayResult.Head.RespCode == "S00000" {
		return ottPayResult, err
	}
	return nil, err
}

func PostTp1022(req *api.Tp1022Req, merchantToken string) (*api.Tp1022Res, error) {
	var (
		err    error
		body   []byte
		resStr []byte
	)
	body, err = json.Marshal(&req)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	resStr, err = OperatorParamToPost("tp1022", merchantToken, body)
	if err != nil {
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	ottPayResult := &api.Tp1022Res{}
	err = json.Unmarshal(resStr, &ottPayResult)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	if ottPayResult.Head.RespDesc == "请求成功" && ottPayResult.Head.RespCode == "S00000" {
		return ottPayResult, err
	}
	return nil, err
}

func PostTp1023(req *api.Tp1023Req, merchantToken string) (*api.Tp1023Res, error) {
	var (
		err    error
		body   []byte
		resStr []byte
	)
	body, err = json.Marshal(&req)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	resStr, err = OperatorParamToPost("tp1023", merchantToken, body)
	if err != nil {
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	ottPayResult := &api.Tp1023Res{}
	err = json.Unmarshal(resStr, &ottPayResult)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	if ottPayResult.Head.RespDesc == "请求成功" && ottPayResult.Head.RespCode == "S00000" {
		return ottPayResult, err
	}
	return nil, err
}

func PostTp3004(req *api.Tp3004Req, merchantToken string) (*api.Tp3004Res, error) {
	var (
		err    error
		body   []byte
		resStr []byte
	)
	body, err = json.Marshal(&req)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	resStr, err = OperatorParamToPost("tp3004", merchantToken, body)
	if err != nil {
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	ottPayResult := &api.Tp3004Res{}
	err = json.Unmarshal(resStr, &ottPayResult)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	if ottPayResult.Head.RespDesc == "请求成功" && ottPayResult.Head.RespCode == "S00000" {
		return ottPayResult, err
	}
	return nil, err
}

func PostTp1002(req *api.Tp1002Req, merchantToken string) (*api.Tp1002Res, error) {
	var (
		err    error
		body   []byte
		resStr []byte
	)
	body, err = json.Marshal(&req)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	resStr, err = OperatorParamToPost("tp1002", merchantToken, body)
	if err != nil {
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	ottPayResult := &api.Tp1002Res{}
	err = json.Unmarshal(resStr, &ottPayResult)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	if ottPayResult.Head.RespDesc == "请求成功" && ottPayResult.Head.RespCode == "S00000" {
		return ottPayResult, err
	}
	return nil, err
}

func PostTp1003(req *api.Tp1003Req, merchantToken string) (*api.Tp1003Res, error) {
	var (
		err    error
		body   []byte
		resStr []byte
	)
	body, err = json.Marshal(&req)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	resStr, err = OperatorParamToPost("tp1003", merchantToken, body)
	if err != nil {
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	ottPayResult := &api.Tp1003Res{}
	err = json.Unmarshal(resStr, &ottPayResult)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	if ottPayResult.Head.RespDesc == "请求成功" && ottPayResult.Head.RespCode == "S00000" {
		return ottPayResult, err
	}
	return nil, err
}

func PostTp1004(req *api.Tp1004Req, merchantToken string) (*api.Tp1004Res, error) {
	var (
		err    error
		body   []byte
		resStr []byte
	)
	body, err = json.Marshal(&req)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	resStr, err = OperatorParamToPost("tp1004", merchantToken, body)
	if err != nil {
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	ottPayResult := &api.Tp1004Res{}
	err = json.Unmarshal(resStr, &ottPayResult)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	if ottPayResult.Head.RespDesc == "请求成功" && ottPayResult.Head.RespCode == "S00000" {
		return ottPayResult, err
	}
	return nil, err
}

func PostTp1005(req *api.Tp1005Req, merchantToken string) (*api.Tp1005Res, error) {
	var (
		err    error
		body   []byte
		resStr []byte
	)
	body, err = json.Marshal(&req)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	resStr, err = OperatorParamToPost("tp1005", merchantToken, body)
	if err != nil {
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	ottPayResult := &api.Tp1005Res{}
	err = json.Unmarshal(resStr, &ottPayResult)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	if ottPayResult.Head.RespDesc == "请求成功" && ottPayResult.Head.RespCode == "S00000" {
		return ottPayResult, err
	}
	return nil, err
}

func PostTp1001(req *api.Tp1001Req, merchantToken string) (*api.Tp1001Res, error) {
	var (
		err    error
		body   []byte
		resStr []byte
	)
	body, err = json.Marshal(&req)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	resStr, err = OperatorParamToPost("tp1001", merchantToken, body)
	if err != nil {
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	ottPayResult := &api.Tp1001Res{}
	err = json.Unmarshal(resStr, &ottPayResult)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	if ottPayResult.Head.RespDesc == "请求成功" && ottPayResult.Head.RespCode == "S00000" {
		return ottPayResult, err
	}
	return nil, err
}

func PostTp1019(req *api.Tp1019Req, merchantToken string) (*api.Tp1019Res, error) {
	var (
		err    error
		body   []byte
		resStr []byte
	)
	body, err = json.Marshal(&req)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	resStr, err = OperatorParamToPost("tp1019", merchantToken, body)
	if err != nil {
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	ottPayResult := &api.Tp1019Res{}
	err = json.Unmarshal(resStr, &ottPayResult)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	if ottPayResult.Head.RespDesc == "请求成功" && ottPayResult.Head.RespCode == "S00000" {
		return ottPayResult, err
	}
	return nil, err
}

func PostTp1012(req *api.Tp1012Req, merchantToken string) (*api.Tp1012Res, error) {
	var (
		err    error
		body   []byte
		resStr []byte
	)
	body, err = json.Marshal(&req)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	resStr, err = OperatorParamToPost("tp1012", merchantToken, body)
	if err != nil {
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	ottPayResult := &api.Tp1012Res{}
	err = json.Unmarshal(resStr, &ottPayResult)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}

	return ottPayResult, err
}

func PostTp6001(req *api.Tp6001Req, merchantToken string) (*api.Tp6001Res, error) {
	var (
		err    error
		body   []byte
		resStr []byte
	)
	body, err = json.Marshal(&req)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	resStr, err = OperatorParamToPost("tp6001", merchantToken, body)
	if err != nil {
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	ottPayResult := &api.Tp6001Res{}
	err = json.Unmarshal(resStr, &ottPayResult)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}

	return ottPayResult, err
}

func PostTp6002(req *api.Tp6002Req, merchantToken string) (*api.Tp6002Res, error) {
	var (
		err    error
		body   []byte
		resStr []byte
	)
	body, err = json.Marshal(&req)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	resStr, err = OperatorParamToPost("tp6002", merchantToken, body)
	if err != nil {
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	ottPayResult := &api.Tp6002Res{}
	err = json.Unmarshal(resStr, &ottPayResult)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}

	return ottPayResult, err
}

func PostTp6003(req *api.Tp6003Req, merchantToken string) (*api.Tp6003Res, error) {
	var (
		err    error
		body   []byte
		resStr []byte
	)
	body, err = json.Marshal(&req)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	resStr, err = OperatorParamToPost("tp6003", merchantToken, body)
	if err != nil {
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	ottPayResult := &api.Tp6003Res{}
	err = json.Unmarshal(resStr, &ottPayResult)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}

	return ottPayResult, err
}

func PostTp6004(req *api.Tp6004Req, merchantToken string) (*api.Tp6004Res, error) {
	var (
		err    error
		body   []byte
		resStr []byte
	)
	body, err = json.Marshal(&req)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	resStr, err = OperatorParamToPost("tp6004", merchantToken, body)
	if err != nil {
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	ottPayResult := &api.Tp6004Res{}
	err = json.Unmarshal(resStr, &ottPayResult)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}

	return ottPayResult, err
}

func PostTp3013(req *api.Tp3013Req, merchantToken string) (*api.Tp3013Res, error) {
	var (
		err    error
		body   []byte
		resStr []byte
	)
	body, err = json.Marshal(&req)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	resStr, err = OperatorParamToPost("tp3013", merchantToken, body)
	if err != nil {
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	ottPayResult := &api.Tp3013Res{}
	err = json.Unmarshal(resStr, &ottPayResult)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}
	return ottPayResult, err
}

func TestTp1001(req *api.TestTp1001Req, merchantToken string) error {
	var (
		err  error
		body []byte
	)
	body, err = json.Marshal(&req)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return internalerrors.New(code.ErrUnknown, err.Error())
	}
	_, err = OperatorParamToPost("test1001", merchantToken, body)
	if err != nil {
		return internalerrors.New(code.ErrUnknown, err.Error())
	}
	return err
}

func TestTp1002(req *api.TestTp1002Req, merchantToken string) error {
	var (
		err  error
		body []byte
	)
	body, err = json.Marshal(&req)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return internalerrors.New(code.ErrUnknown, err.Error())
	}
	_, err = OperatorParamToPost("test1002", merchantToken, body)
	if err != nil {
		return internalerrors.New(code.ErrUnknown, err.Error())
	}
	return err
}

func Tp1014(req *api.Tp1014Req, merchantToken string) error {
	var (
		err  error
		body []byte
	)
	body, err = json.Marshal(&req)
	if err != nil {
		logger.SugarLogger.Errorf("json转换:%w", err)
		return internalerrors.New(code.ErrUnknown, err.Error())
	}
	_, err = OperatorParamToPost("tp1014", merchantToken, body)
	if err != nil {
		return internalerrors.New(code.ErrUnknown, err.Error())
	}
	return err
}

func GetPayeeType(ctx context.Context, payeeType string) ([]*api.PayeeTypeRes, error) {
	var (
		err          error
		payeeTypeObj []*api.PayeeTypeRes
		typeCode     string
	)

	if payeeType == "" {
		logger.SugarLogger.Errorf("收款人类型为空")
		return []*api.PayeeTypeRes{}, internalerrors.New(code.ErrUnknown, "请选择收款人类型")
	}
	typeCode = "企业"
	if payeeType == "00" {
		typeCode = "个人"
	}

	if err = global.GFDB.Model("ifcgate_payee_type_base").Where("payee_type=?", typeCode).Scan(&payeeTypeObj); err != nil {
		logger.SugarLogger.Error(err)
		return []*api.PayeeTypeRes{}, internalerrors.New(code.ErrUnknown, "数据查询异常")
	}

	if payeeTypeObj == nil {
		return []*api.PayeeTypeRes{}, nil
	}

	return payeeTypeObj, nil
}

// 汇款用途
func GetTradePurpose(ctx context.Context) ([]*api.TradePurposeRes, error) {
	var (
		err             error
		tradePurposeObj []*model.IFCGateTradePurpose
	)
	if err = global.GFDB.Ctx(ctx).Model("ifcgate_trade_purpose_base").Scan(&tradePurposeObj); err != nil {
		logger.SugarLogger.Error(err)
		return nil, internalerrors.New(code.ErrUnknown, err.Error())
	}

	if tradePurposeObj == nil {
		return []*api.TradePurposeRes{}, nil
	}

	result := make([]*api.TradePurposeRes, 0)
	for _, value := range tradePurposeObj {
		result = append(result, &api.TradePurposeRes{
			Code: value.Code,
			Desc: value.Desc,
		})
	}
	return result, err
}

func OperatorImageToSftp(str string) string {
	tarGetPath := global.ViperObj.GetString("sftp.targetPath")
	lastPath := strings.LastIndex(str, "/")
	subStr := str[lastPath+1:]
	return fmt.Sprintf("%s%s%s", tarGetPath, "/", subStr)
}

func OperatorImageToSftpArray(strArr []string) []string {
	tarGetPath := global.ViperObj.GetString("sftp.targetPath")
	sftpArr := make([]string, 0)
	for _, value := range strArr {
		lastPath := strings.LastIndex(value, "/")
		subStr := value[lastPath+1:]
		sftpArr = append(sftpArr, fmt.Sprintf("%s%s%s", tarGetPath, "/", subStr))
	}
	return sftpArr
}

func GetPurposeList(ctx context.Context) ([]*api.PurposeRes, error) {
	var (
		err        error
		purposeObj []*model.IFCPurpose
	)

	if err = global.GFDB.Model("ifcgate_purpose_base").Scan(&purposeObj); err != nil {
		logger.SugarLogger.Error(err)
		return []*api.PurposeRes{}, internalerrors.New(code.ErrUnknown, "数据查询异常")
	}
	if purposeObj == nil {
		logger.SugarLogger.Error(err)
		return []*api.PurposeRes{}, nil
	}

	purposeList := make([]*api.PurposeRes, 0)
	for _, value := range purposeObj {
		purposeList = append(purposeList, &api.PurposeRes{
			Code: value.Code,
			Desc: api.DescList{
				DescZh: value.DescZh,
				DescEn: value.DescEn,
			},
		})
	}

	return purposeList, nil
}

func CheckInvitationCode(ctx context.Context, invitationCode string) (bool, error) {
	var (
		err error
	)
	num, err := global.GFDB.Model("ifcgate_invitation_code").Where("invitation_code", invitationCode).Count()
	if err != nil {
		return false, internalerrors.New(code.ErrUnknown, "数据查询异常")
	}
	if num <= 0 {
		return false, nil
	}

	return true, nil
}
